package com.lin.controller;

import com.lin.VO.ResultVO;
import com.lin.dataobject.Role;
import com.lin.enums.ResultEnum;
import com.lin.exception.SellException;
import com.lin.form.RoleForm;
import com.lin.server.RoleService;
import com.lin.utils.ResultVOUtils;
import com.lin.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by MYQ on 2017/10/27.
 */
@Controller
@RequestMapping("/role")
public class SellerRoleController {

    @Autowired
    private RoleService service;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("role/index");
    }

    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest request1=new PageRequest(page-1,rows);
        String name=request.getParameter("name");
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        Map<String,Object> map=new HashMap<>();
        Page<Role> rolePage=service.findAll(name,startTime,endTime,request1);
        map.put("rows",rolePage.getContent());
        map.put("total",service.getSize(name,startTime,endTime));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) String id,
                                Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            Role role=service.findOne(Integer.valueOf(id));
            map.put("role",role);
        }
        return new ModelAndView("role/add_view");
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(@Valid RoleForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        Role role=new Role();
        try{
            if (form.getId()!=null){
                role=service.findOne(form.getId());
            }
            BeanUtils.copyProperties(form,role);
            service.save(role);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("/del")
    public ResultVO del(@RequestParam(value = "id",required = false) String id){
        Role role=service.findOne(Integer.valueOf(id));
        if (role==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        service.del(role);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/goAccess")
    public ModelAndView goAccess(@RequestParam(value = "id",required = true) String id,
                                Map<String,Object> map){
        map.put("id",id);
        return new ModelAndView("role/access");
    }

    @ResponseBody
    @PostMapping("/saveAccess")
    public ResultVO saveAccess(@RequestParam(value = "id") Integer id,
                               @RequestParam(value = "ids") String ids){
        Role role=service.findOne(Integer.valueOf(id));
        if (role==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        role.setAccess(ids);
        service.save(role);
        return ResultVOUtils.success("配置成功");
    }
}
